#include "head_c.h"

int log_grand_set(LOG_GRADE_t log_grand,char* log_grand_global, char* write_buf) {

    //最低等级DEBUG
    if((log_grand == LOG_DEBUG ||
        log_grand == LOG_INFO ||
        log_grand == LOG_WARNINGS ||
        log_grand == LOG_ERROR) &&
       strcmp(log_grand_global, "LOG_DEBUG") == 0
      ) {
        ssize_t len = strlen(write_buf);
        printf("%s", write_buf);
        int log_fd = open("client_log.txt", O_RDWR | O_APPEND | O_CREAT, 0666);
        write(log_fd, write_buf, len);
        close(log_fd);
    }
    //等级为INFO
    else if((log_grand == LOG_INFO ||
             log_grand == LOG_WARNINGS ||
             log_grand == LOG_ERROR) &&
            strcmp(log_grand_global,"LOG_INFO") == 0
           ) {
        ssize_t len = strlen(write_buf);
        printf("%s", write_buf);
        int log_fd = open("client_log.txt", O_RDWR | O_APPEND | O_CREAT, 0666);
        write(log_fd, write_buf, len);
        close(log_fd);
    }
    else if((log_grand == LOG_WARNINGS ||
             log_grand == LOG_ERROR) &&
            strcmp(log_grand_global,"LOG_WARNINGS") == 0
           ) {
        ssize_t len = strlen(write_buf);
        printf("%s", write_buf);
        int log_fd = open("client_log.txt", O_RDWR | O_APPEND | O_CREAT, 0666);
        write(log_fd, write_buf, len);
        close(log_fd);
    }
    else if(log_grand == LOG_ERROR && strcmp(log_grand_global,"LOG_ERROR") == 0) {
        ssize_t len = strlen(write_buf);
        printf("%s", write_buf);
        int log_fd = open("client_log.txt", O_RDWR | O_APPEND | O_CREAT, 0666);
        write(log_fd, write_buf, len);
        close(log_fd);
    }
    else{
        return -1;
    }

    return 0;
}
